package com.holyland.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.holyland.model.Matrix;
import com.holyland.model.Member;
import com.holyland.model.User;
import com.holyland.service.MemberService;
import com.holyland.util.HolylandException;

import static com.holyland.util.IHolylandConstants.SESSION_USER;

public class MatrixAction extends BaseAction {	
	private static final long serialVersionUID = -2763410135059493045L;

	private String email;
	
	private List<Member> listMembers;	
	private Matrix matrix;
	
	private Member member;
	
	private Long memberId;		

	public List<Member> getListMembers() {
		return listMembers;
	}

	public void setListMembers(List<Member> listMembers) {
		this.listMembers = listMembers;
	}

	public Matrix getMatrix() {
		return matrix;
	}

	public void setMatrix(Matrix matrix) {
		this.matrix = matrix;
	}

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}		
	
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	@SkipValidation
	public String showMatrix() {
		
		User user = (User) session.get(SESSION_USER);
		
		if(user != null) {
			boolean isMember = user.isAdmin();
			
			if(!isMember) {
				
				memberId = user.getMemberId();				
				return "viewMatrix";				
			}
		}
		
		return FWD_SUCCESS;
	}
	
	public String searchMatrix() {
		Member search = new Member();
		search.setEmail(email);
		search.setCycledOut(false);
		
		listMembers = MemberService.getInstance().searchMember(search);
		
		return FWD_SUCCESS;
	}
	
	@SkipValidation
	public String viewMatrix() throws HolylandException {
		member = MemberService.getInstance().findMemberById(memberId);
		
		if(member != null) {
			matrix = MemberService.getInstance().searchMatrix(memberId);
		}
		
		return FWD_SUCCESS;
	}

	@Override
	public void validate() {
		if(StringUtils.isBlank(email)) {
			addActionError(getText("errRequired", new String[]{"Email"}));
		}
	}		
}
